using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ServerSelectorView : MonoBehaviour
{
    private Action actionCallback;
    private Button btn_Reload;
    private InputField inputField;
    private InputField portField;
    private string ip;
    private string port;
    private void Awake()
    {
        btn_Reload = this.transform.Find("Button").GetComponent<Button>();
        inputField = this.transform.Find("InputField").GetComponent<InputField>();
        portField = this.transform.Find("port").GetComponent<InputField>();

        ip = PlayerPrefs.GetString("ip", "127.0.0.1");
        port = PlayerPrefs.GetString("prot", "80");


        inputField.text = ip;
        portField.text = port;

    }
    // Start is called before the first frame update
    void Start()
    {
        btn_Reload.onClick.AddListener(() => {
            if(ip == null)
            {
                return;
            }
            PlayerPrefs.SetString("ip", ip);
            PlayerPrefs.SetString("port", port);
            Const.RES_IP = $"http://{ip}:{port}/";
            this.actionCallback?.Invoke();
        });
        inputField.onValueChanged.AddListener((ip) => {
            this.ip = ip;
        });
        portField.onValueChanged.AddListener((port) => {
            this.port = port;
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void AddSelectCallBack(Action p)
    {
        this.actionCallback = p;
    }
}
